#include "MusicController.h"

enum {
	kMusicPlaying, kMusicPause, kMusicStop,
};

static MusicController * g_controller = NULL;

MusicController::MusicController() :
		m_musicPath(0), m_musicState(kMusicStop) {

}

MusicController::~MusicController() {

}

void MusicController::init() {
	m_bMusicOn = CCUserDefault::sharedUserDefault()->getBoolForKey("music_on",
			true);
	m_bEffectOn = CCUserDefault::sharedUserDefault()->getBoolForKey("effect_on",
			false);
}

void MusicController::preloadMusic(const char * path) {
	m_musicState = kMusicStop;
	SimpleAudioEngine::sharedEngine()->preloadBackgroundMusic(path);

}
void MusicController::startMusic(const char *path, bool repeat) {
	if (m_bMusicOn) {
		m_musicPath = path;
		m_musicState = kMusicPlaying;
		SimpleAudioEngine::sharedEngine()->playBackgroundMusic(path, repeat);
	}
}
void MusicController::resumeMusic() {
	if (m_bMusicOn) {
		m_musicState = kMusicPlaying;
		SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();
	}
}
void MusicController::pauseMusic() {
	if (m_bMusicOn) {
		m_musicState = kMusicPause;
		SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();
	}
}
void MusicController::stopMusic() {
	if (m_bMusicOn) {
		m_musicState = kMusicStop;
		SimpleAudioEngine::sharedEngine()->stopBackgroundMusic();
	}
}
void MusicController::releaseMusic(const char * path) {
	//SimpleAudioEngine::sharedEngine()->unloadBackgroundMusic(path);
}

void MusicController::preloadEffect(const char * path) {
	SimpleAudioEngine::sharedEngine()->preloadEffect(path);
}
int MusicController::startEffect(const char *path, bool repeat) {
	if (m_bEffectOn)
		return SimpleAudioEngine::sharedEngine()->playEffect(path, repeat);
	else
		return -1;
}
void MusicController::resumeEffect(int songId) {
	if (m_bEffectOn)
		SimpleAudioEngine::sharedEngine()->resumeEffect(songId);
}
void MusicController::pauseEffect(int songId) {
	if (m_bEffectOn)
		SimpleAudioEngine::sharedEngine()->pauseEffect(songId);
}
void MusicController::stopEffect(int songId) {
	if (m_bEffectOn)
		SimpleAudioEngine::sharedEngine()->stopEffect(songId);
}

void MusicController::releaseEffect(int songId) {
	//SimpleAudioEngine::sharedEngine()->unloadEffect(path);
}

void MusicController::setMusicOn(bool on) {
	if (on) {
		m_bMusicOn = on;
		switch (m_musicState) {
		case kMusicPlaying:
			break;
		case kMusicPause:
			resumeMusic();
			break;
		case kMusicStop:
			if (m_musicPath) {
				startMusic(m_musicPath, true);
			}
			break;
		}

	} else {
		switch (m_musicState) {
		case kMusicPlaying:
			pauseMusic();
			break;
		case kMusicPause:
			break;
		case kMusicStop:
			break;
		}
		m_bMusicOn = on;
	}

	CCUserDefault::sharedUserDefault()->setBoolForKey("music_on", on);
	CCUserDefault::sharedUserDefault()->flush();
}

void MusicController::setEffectOn(bool on) {
	m_bEffectOn = on;
	CCUserDefault::sharedUserDefault()->setBoolForKey("effect_on", on);
	CCUserDefault::sharedUserDefault()->flush();
}

MusicController * MusicController::getInstance() {
	if (!g_controller) {
		g_controller = new MusicController();
		g_controller->init();
	}
	return g_controller;
}

void MusicController::release() {
	if (g_controller) {
		SimpleAudioEngine::sharedEngine()->end();
		delete g_controller;
	}
}

